package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ShoppingcartMapper;
import com.sky.mapper.StemealDishMapper;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingcartMapper shoppingcartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    StemealDishMapper stemealDishMapper;

    //添加购物车
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //查询购物车中 是否存在当前的商品
        ShoppingCart shoppingCartCondition = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCartCondition);//拷贝菜品
        shoppingCartCondition.setUserId(BaseContext.getCurrentId());//设置用户id
        //查询购物车表
        List<ShoppingCart> shoppingCartList = shoppingcartMapper.selectByCondition(shoppingCartCondition);
        //查询到购物车已经存在当前商品 更新商品的数量
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //更新商品的数量
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingcartMapper.update(shoppingCart);
        } else {
            //,没有查询到的话 添加商品到购物车当中
            //判断当前添加的是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                //添加的是菜品信息
                shoppingCartCondition.setName(dish.getName());
                shoppingCartCondition.setImage(dish.getImage());
                shoppingCartCondition.setAmount(dish.getPrice());

            } else {
                Setmeal setmeal = stemealDishMapper.queryById(shoppingCartDTO.getSetmealId());
                shoppingCartCondition.setAmount(setmeal.getPrice());
                shoppingCartCondition.setName(setmeal.getName());
                shoppingCartCondition.setImage(setmeal.getImage());
            }
            shoppingCartCondition.setNumber(1);//默认商品数量等于1
            shoppingCartCondition.setCreateTime(LocalDateTime.now());
            shoppingcartMapper.insert(shoppingCartCondition);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingcartMapper.selectByCondition(shoppingCart);
        return shoppingCartList;
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingcartMapper.clean(shoppingCart);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingcartMapper.selectByCondition(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            if (shoppingCartList.get(0).getNumber() <= 1) {
                shoppingcartMapper.deleteById(shoppingCartList.get(0).getId());
            } else {
                ShoppingCart shoppingCart1 = shoppingCartList.get(0);
                shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
                shoppingcartMapper.update(shoppingCart1);
            }
        }
    }
}
